package com.own.demo.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.utils.geo.buffer.BufferBase;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 对角线url
 *
 * @author heguanglong
 */
public class DiagonalUtils {

    public static List<List<List<Coordinate>>> diagonalGeo(List<List<Coordinate>> allPint02, List<Coordinate> centerPoint02, List<List<Coordinate>> allPint13) {
        //所有对角线点
        List<List<List<Coordinate>>> all = new ArrayList<>();
        all.addAll(diagonalGeo(allPint02, centerPoint02));
        all.addAll(diagonalGeo(allPint13, centerPoint02));
//        all.add(allPint02);
//        all.add(allPint13);
        return all;
    }

    public static List<Polygon> diagonalGeoPl(List<Polygon> allPint02, List<Coordinate> centerPoint02) {
        for (int h = 0; h < centerPoint02.size(); h++) {

        }
        return null;
    }

    public static List<List<List<Coordinate>>> diagonalGeo(List<List<Coordinate>> allPint02, List<Coordinate> centerPoint02) {
        //所有对角线点
        List<List<List<Coordinate>>> all = new ArrayList<>();

        for (int h = 0; h < centerPoint02.size(); h++) {
            Coordinate coordinate1 = centerPoint02.get(h);

            List< List<Coordinate>> indexList=new ArrayList<>(1024);
            List<List<Coordinate>> temp = new ArrayList<>();
            for (int i = 0; i < allPint02.size(); i++) {
                List<Coordinate> list = allPint02.get(i);
                boolean flag = GeoTools.onLine(coordinate1, list.get(0), list.get(1));
                if (flag) {
                    temp.add(list);
                    indexList.add(list);
                }
            }

            allPint02.removeAll(indexList);

            if (temp.size() > 0) {
                all.add(temp);
            }
        }
        return all;
    }

    public static String dataDrawInfo(List<List<List<Coordinate>>> listInfo) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray(1024);
        jsonObject.put("type", "GeometryCollection");
        int row = listInfo.size();

        for (int j = 0; j < row; j++) {
            List<List<Coordinate>> listTemp=listInfo.get(j);
            for (int i = 0; i < listTemp.size(); i++) {
                List<Coordinate> coordinates=  listTemp.get(i);
                jsonArray.add(setGeoJSON(i, j, coordinates, "LineString"));
            }

        }

        jsonObject.put("features", jsonArray);
        return jsonObject.toJSONString();
    }

    public static JSONObject setGeoJSON(int i, int j, List<Coordinate> list, String geoType) {
        JSONObject geo = new JSONObject(4);
        geo.put("type", "Feature");
        JSONObject geoPro = new JSONObject(1);
        geoPro.put("name", i > 0 ? j + "-" + i : "area");
        geo.put("properties", geoPro);
        JSONObject objectInfo = new JSONObject();
        Coordinate[] coordinates = list.toArray(new Coordinate[0]);
        int coordRow = coordinates.length;
        List<double[]> gpsList = new ArrayList<>(coordRow);

        for (int h = 0; h < coordRow; h++) {
            Coordinate coordinate = coordinates[h];
            double[] gps = new double[2];
            gps[0] = coordinate.x;
            gps[1] = coordinate.y;
            gpsList.add(gps);
        }
        objectInfo.put("coordinates", gpsList);
        objectInfo.put("type", geoType);
        geo.put("geometry", objectInfo);
        return geo;
    }

    /**
     * 右对角线
     * @param polygons
     * @return
     */
    public static List<List<Coordinate>> rightDiagonaLine(Coordinate[][] polygons, Geometry geometry, Map<String,Polygon> pointPolygon){
        int rows=polygons.length;
        List<List<Coordinate>> listPoints=new ArrayList<>(rows);
        int  cols = polygons[0].length;
        for(int i=0;i<cols * 4 - 1;i++){
            int   diff = cols - i - 1;
            List<Coordinate> listTemp=new ArrayList<>();
            for(int j=0;j<cols;j++){
                int  k = j - diff;
                if(isValidIndex(k, rows)){
                    try {
                        Coordinate coordinate= polygons[k][j];
                        Point point= BufferBase.geometryFactory.createPoint(coordinate);
                        if (point.within(geometry)||point.intersects(geometry)){
                            String key=point.getX()+"_"+point.getY();
                            Polygon polygon= pointPolygon.get(key);
                            Coordinate coordinate1=polygon.getCoordinates()[2];
                            listTemp.add(coordinate1);
                            listTemp.add(coordinate);
                            Coordinate coordinate4=polygon.getCoordinates()[4];
                            listTemp.add(coordinate4);
                        }
                    }catch (Exception e){

                    }
                }
            }
            listTemp = listTemp.stream().distinct().collect(Collectors.toList());
            listTemp = listTemp.stream().sorted(Comparator.comparing(Coordinate::getX)).collect(Collectors.toList());
            if(listTemp.size()>1) {
                Collections.reverse(listTemp);
                listPoints.add(listTemp);
            }
        }
        return listPoints;
    }

    /**
     * 右对角线
     * @param polygons
     * @return
     */
    public static List<List<Coordinate>> leftDiagonaLine(Coordinate[][] polygons, Geometry geometry,Map<String,Polygon> pointPolygon){
        int rows=polygons.length;
        List<List<Coordinate>> listPoints=new ArrayList<>(rows);
        for(int i=0;i<(rows+rows-1);i++){
            List<Coordinate> listTemp=new ArrayList<>();
            for (int j=0;j<i+1;j++){
                int  k = i-j;
                if (k<rows && k>=0 && j<rows){
                    try {
                        Coordinate coordinate=  polygons[j][k];
                        Point point= BufferBase.geometryFactory.createPoint(coordinate);
                        if (point.within(geometry)||point.intersects(geometry)) {
                            String key=point.getX()+"_"+point.getY();
                            Polygon polygon= pointPolygon.get(key);
                            Coordinate coordinate3=polygon.getCoordinates()[3];
                            listTemp.add(coordinate3);
                            listTemp.add(coordinate);

                            Coordinate coordinate1=polygon.getCoordinates()[1];
                            listTemp.add(coordinate1);
                        }
                    }catch (Exception e){
                    }
                }
            }
            listTemp = listTemp.stream().distinct().collect(Collectors.toList());
            listTemp = listTemp.stream().sorted(Comparator.comparing(Coordinate::getX)).collect(Collectors.toList());
            if(listTemp.size()>1) {
                listPoints.add(listTemp);
            }
        }
        return listPoints;
    }

    /**
     * 右对角线
     * @param polygons
     * @return
     */
    public static List<List<Coordinate>> rightDiagonaPolygons(Polygon[][] polygons, Geometry geometry){
        int rows=polygons.length;
        List<List<Coordinate>> listPoints=new ArrayList<>(rows);
        int  cols = polygons[0].length;
        for(int i=0;i<cols * 4 - 1;i++){
            int   diff = cols - i - 1;
            List<Coordinate> listTemp=new ArrayList<>();
            for(int j=0;j<cols;j++){
                int  k = j - diff;
                if(isValidIndex(k, rows)){
                    try {
                        Polygon coordinate= polygons[k][j];
//                        Point point= BufferBase.geometryFactory.createPoint(coordinate);
                        if (coordinate.within(geometry) || coordinate.intersects(geometry)){
                            Coordinate[] coordinates=  coordinate.getCoordinates();
                            Coordinate co1=coordinates[0];
                            Coordinate co3=coordinates[2];
                            listTemp.add(co1);
                            listTemp.add(co3);
                        }
                    }catch (Exception e){

                    }
                }
            }
            listTemp = listTemp.stream().distinct().collect(Collectors.toList());
            listTemp = listTemp.stream().sorted(Comparator.comparing(Coordinate::getY)).collect(Collectors.toList());
            listPoints.add(listTemp);
        }
        return listPoints;
    }

    /**
     * 右对角线
     * @param polygons
     * @return
     */
    public static List<List<Coordinate>> leftDiagonaPolygons(Polygon[][] polygons, Geometry geometry){
        int rows=polygons.length;
        List<List<Coordinate>> listPoints=new ArrayList<>(rows);
//        for(int i=0;i<(rows+rows-1);i++){
//            List<Coordinate> listTemp=new ArrayList<>();
//            for (int j=0;j<i+1;j++){
//                int  k = i-j;
//                if (k<rows && k>=0 && j<rows){
//                    try {
//                        Coordinate coordinate=  polygons[j][k];
//                        Point point= BufferBase.geometryFactory.createPoint(coordinate);
//                        if (point.within(geometry)||point.intersects(geometry)) {
//                            listTemp.add(coordinate);
//                        }
//                    }catch (Exception e){
//                    }
//                }
//            }
//            listTemp = listTemp.stream().distinct().collect(Collectors.toList());
//            listTemp = listTemp.stream().sorted(Comparator.comparing(Coordinate::getX)).collect(Collectors.toList());
//            listPoints.add(listTemp);
//        }
        return listPoints;
    }


    public static boolean  isValidIndex(int x, int n){
        return (x >= 0 && x < n);
    }

}
